package com.example.course.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.course.dto.ActivityDTO;
import com.example.course.service.ActivityService;
import com.example.course.tool.R;
import com.example.course.vo.ActivityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 活动控制器
 *
 * @author Jasmine-drs
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/activities")
@Api(tags = "活动管理接口")
public class ActivityController {


	private final ActivityService activityService;

	public ActivityController(ActivityService activityService) {
		this.activityService = activityService;
	}

	/**
	 * 获取活动列表
	 *
	 * @param current 当前页
	 * @param size    每页大小
	 * @return 活动列表
	 */
	@GetMapping
	@ApiOperation("获取活动列表")
	public R<Page<ActivityVO>> listActivities(
			@ApiParam(value = "当前页", defaultValue = "1") @RequestParam(defaultValue = "1") long current,
			@ApiParam(value = "每页大小", defaultValue = "10") @RequestParam(defaultValue = "10") long size) {
		try {
			Page<ActivityVO> page = activityService.listActivities(current, size);
			return R.success(page);
		} catch (Exception e) {
			return R.err("获取活动列表失败：" + e.getMessage());
		}
	}

	/**
	 * 获取活动详情
	 *
	 * @param id 活动ID
	 * @return 活动详情
	 */
	@GetMapping("/{id}")
	@ApiOperation("获取活动详情")
	public R<ActivityVO> getActivityById(
			@ApiParam(value = "活动ID", required = true, example = "1") @PathVariable Integer id) {
		try {
			ActivityVO activityVO = activityService.getActivityById(id);
			return R.success(activityVO);
		} catch (Exception e) {
			return R.err("获取活动详情失败：" + e.getMessage());
		}
	}

	/**
	 * 添加活动
	 *
	 * @param activityDTO 活动信息
	 * @return 添加结果
	 */
	@PostMapping
	@ApiOperation("添加活动")
	public R<ActivityVO> addActivity(
			@ApiParam(value = "活动信息", required = true) @Validated @RequestBody ActivityDTO activityDTO) {
		try {
			ActivityVO activityVO = activityService.addActivity(activityDTO);
			return R.success(activityVO);
		} catch (Exception e) {
			return R.err("添加活动失败：" + e.getMessage());
		}
	}
} 